package dbcontext;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import dbhelpers.AlbumHelper;
import dbhelpers.PhotoHelper;
import dbhelpers.UserHelper;
import dbmodels.Album;
import dbmodels.Photo;
import dbmodels.User;
import dbtypes.PermisionAlbum;
import dbtypes.PermisionPhoto;

public class DBContext {

	private Helper helper;
	private SQLiteDatabase db;
	private ContentValues c;

	public DBContext(Context context) {

		helper = new Helper(context);
		try {
			db = helper.getWritableDatabase();
		} catch (Exception e) {
		}

	}

	public boolean insert(User u) {
		c = new ContentValues();

		c.put("id", u.getId().toString());
		c.put("name", u.getName());
		c.put("pass", u.getPass());

		return insert(UserHelper.nameTable, c);
	}

	public boolean insert(Album a) {
		c = new ContentValues();

		c.put("id", a.getId().toString());
		c.put("name", a.getName());
		c.put("permision", a.getPermision().toInt());
		c.put("userid", a.getUserid().toString());

		return insert(AlbumHelper.nameTable, c);
	}

	public boolean insert(Photo p) {
		c = new ContentValues();

		c.put("id", p.getId().toString());
		c.put("bytes", p.getBytes());
		c.put("name", p.getName());
		c.put("albumid", p.getAlbumid().toString());
		c.put("permision", p.getPermision().toInt());

		return insert(PhotoHelper.nameTable, c);
	}

	@SuppressWarnings("deprecation")
	public List<User> getAllUsers() {

		List<User> users = new ArrayList<User>();
		Cursor cursor = db.query(UserHelper.nameTable, null, null, null, null,
				null, null);

		cursor.moveToFirst();
		User u;
		while (!cursor.isAfterLast()) {
			u = new User();
			u.setName(cursor.getString(cursor.getColumnIndex("name")));
			u.setId(UUID.fromString(cursor.getString(cursor
					.getColumnIndex("id"))));
			u.setPass(cursor.getString(cursor.getColumnIndex("pass")));
			users.add(u);
			cursor.moveToNext();
		}
		cursor.deactivate();
		return users;
	}

	@SuppressWarnings("deprecation")
	public List<Album> getAllAlbums() {

		List<Album> albums = new ArrayList<Album>();
		Cursor cursor = db.query(AlbumHelper.nameTable, null, null, null, null,
				null, null);

		cursor.moveToFirst();
		Album a;
		PermisionAlbum pa = PermisionAlbum.All;
		while (!cursor.isAfterLast()) {
			a = new Album();
			a.setName(cursor.getString(cursor.getColumnIndex("name")));
			a.setId(UUID.fromString(cursor.getString(cursor
					.getColumnIndex("id"))));
			a.setUserid(UUID.fromString(cursor.getString(cursor
					.getColumnIndex("userid"))));
			a.setPermision(pa.getVal(cursor.getInt(cursor
					.getColumnIndex("permision"))));
			cursor.moveToNext();
			albums.add(a);
		}
		cursor.deactivate();
		return albums;
	}

	@SuppressWarnings("deprecation")
	public List<Photo> getAllPhotos() {

		List<Photo> photos = new ArrayList<Photo>();
		Cursor cursor = db.query(PhotoHelper.nameTable, null, null, null, null,
				null, null);

		cursor.moveToFirst();
		Photo p;
		PermisionPhoto pp = PermisionPhoto.All;
		while (!cursor.isAfterLast()) {
			p = new Photo();
			p.setName(cursor.getString(cursor.getColumnIndex("name")));
			p.setId(UUID.fromString(cursor.getString(cursor
					.getColumnIndex("id"))));
			p.setAlbumid(UUID.fromString(cursor.getString(cursor
					.getColumnIndex("albumid"))));
			p.setPermision(pp.getVal(cursor.getInt(cursor
					.getColumnIndex("permision"))));
			p.setBytes(cursor.getBlob(cursor.getColumnIndex("bytes")));
			photos.add(p);
			cursor.moveToNext();
		}

		cursor.deactivate();
		return photos;
	}

	@SuppressWarnings("deprecation")
	public User getUserById(UUID id) {
		Cursor cursor = db.query(UserHelper.nameTable, null, "id = ?",
				new String[] { id.toString() }, null, null, null);

		cursor.moveToFirst();
		User u = null;
		if (cursor != null && cursor.getCount() > 0) {
			u = new User();
			u.setName(cursor.getString(cursor.getColumnIndex("name")));
			u.setId(UUID.fromString(cursor.getString(cursor
					.getColumnIndex("id"))));
			u.setPass(cursor.getString(cursor.getColumnIndex("pass")));
		}
		cursor.deactivate();
		return u;
	}

	@SuppressWarnings("deprecation")
	public Album getAlbumById(UUID id) {
		Cursor cursor = db.query(AlbumHelper.nameTable, null, "id = ?",
				new String[] { id.toString() }, null, null, null);

		cursor.moveToFirst();

		Album a = null;
		if (cursor != null && cursor.getCount() > 0) {
			a = new Album();
			PermisionAlbum pa = PermisionAlbum.All;
			a.setName(cursor.getString(cursor.getColumnIndex("name")));
			a.setId(UUID.fromString(cursor.getString(cursor
					.getColumnIndex("id"))));
			a.setUserid(UUID.fromString(cursor.getString(cursor
					.getColumnIndex("userid"))));
			a.setPermision(pa.getVal(cursor.getInt(cursor
					.getColumnIndex("permision"))));
		}
		cursor.deactivate();
		return a;
	}

	@SuppressWarnings("deprecation")
	public Photo getPhotoById(UUID id) {
		Cursor cursor = db.query(PhotoHelper.nameTable, null, "id = ?",
				new String[] { id.toString() }, null, null, null);

		cursor.moveToFirst();
		Photo p = null;
		PermisionPhoto pp = PermisionPhoto.All;
		if (cursor != null && cursor.getCount() > 0) {
			p = new Photo();
			p.setName(cursor.getString(cursor.getColumnIndex("name")));
			p.setId(UUID.fromString(cursor.getString(cursor
					.getColumnIndex("id"))));
			p.setAlbumid(UUID.fromString(cursor.getString(cursor
					.getColumnIndex("albumid"))));
			p.setPermision(pp.getVal(cursor.getInt(cursor
					.getColumnIndex("permision"))));
			p.setBytes(cursor.getBlob(cursor.getColumnIndex("bytes")));
		}
		cursor.deactivate();
		return p;
	}

	public boolean deleteAlbum(UUID albumid) {
		try {
			db.delete(AlbumHelper.nameTable, "id = ?",
					new String[] { albumid.toString() });
		} catch (Exception e) {
			return false;
		}

		return true;
	}

	public boolean deletePhoto(UUID photoid) {
		try {
			db.delete(PhotoHelper.nameTable, "id = ?",
					new String[] { photoid.toString() });
		} catch (Exception e) {
			return false;
		}

		return true;
	}

	public boolean updateAlbum(Album a) {
		c = new ContentValues();
		c.put("id", a.getId().toString());
		c.put("name", a.getName());
		c.put("permision", a.getPermision().toInt());
		c.put("userid", a.getUserid().toString());

		db.update(AlbumHelper.nameTable, c, "id = ?", new String[] { a.getId()
				.toString() });

		return true;

	}

	public boolean updatePhoto(Photo p){
		c.put("id", p.getId().toString());
		c.put("bytes", p.getBytes());
		c.put("name", p.getName());
		c.put("albumid", p.getAlbumid().toString());
		c.put("permision", p.getPermision().toInt());
		db.update(PhotoHelper.nameTable, c, "id = ?", new String[]{ p.getId().toString()});
		
		return true;
	}

	private boolean insert(String name, ContentValues c) {
		try {
			db.insert(name, null, c);
		} catch (Exception e) {
			return false;
		}

		return true;
	}
}
